package com.cheerscpl;

import com.cheerscpl.redis.JedisClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.redis.core.RedisTemplate;

/**
 * Created by chenpuliang on 2017/6/4.
 */
public class TaskQueue4Redis implements TaskQueue{

    private static final Logger logger = LoggerFactory.getLogger(TaskQueue4Redis.class);

    private static RedisTemplate redisTemplate = JedisClient.getRedisTemplate();

    private final String name;

    /**
     * 构造方法
     * @param name
     */
    public TaskQueue4Redis(String name) {
        this.name = name;
    }

    @Override
    public String getName() {
        return this.name;
    }

    @Override
    public void pushTask(Task task) {
        logger.debug("push queue: {}", task.getName());
        redisTemplate.opsForList().leftPush(this.name, task);
    }

    @Override
    public Task popTask() {
        logger.debug("pop queue: {}", this.name);
        return (Task) redisTemplate.opsForList().rightPop(this.name);
    }
}
